home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr25 / wordy.zip / RF.C < prev    next >
C/C++ Source or Header  |  1995-04-29  |  6KB  |  210 lines

  1. /**************************************************************************/
  2. /*                             Reformat Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                         */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /*      Reformats the files created by the pattern matching utilities     */
  13. /*                  so that they print in  columns of 5.                  */
  14. /*                                                                        */
  15. /**************************************************************************/
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <conio.h>
  20.  
  21.  
  22. #define FILE_OPENING_ERROR 3
  23. #define FILENAME_MAXLEN 8
  24. #define CR "\n"
  25. #define MAXLEN 82
  26. #define LINE_LEN 80
  27. #define NOARGS 1
  28. #define INCREMENT 1
  29. #define SPACE ' '
  30. #define MAXWORDLEN 20
  31. #define WORDSPACING 3
  32. //#define COLUMNS 5 (see below)
  33. #define BUFFERSIZE 4096
  34.  
  35.  
  36. void reformat( char *fname, int maxwordlength );
  37. int get_max_word_length( char *fname );
  38. void center( char *strg ); 
  39.  
  40. char ad[] =
  41. "REFORMAT utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  42.  
  43.  
  44.  
  45. typedef enum { FALSE, TRUE } Boolean;
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char filename[ MAXLEN ];
  51.    int mwl;
  52.  
  53.      if( argc == NOARGS )
  54.         {
  55.         clrscr();
  56.         puts("Enter a FILENAME to reformat... ");
  57.         gets( filename );
  58.         }
  59.      else
  60.         strcpy( filename, *( argv + 1 ) );
  61.  
  62.      mwl = get_max_word_length( filename );
  63.      reformat( filename, mwl );
  64. }
  65.  
  66.  
  67.  
  68.  
  69. /*************************************************************/
  70. void reformat( char *filename, int maxwl )
  71. {
  72.  
  73.     char    word [ MAXLEN ],
  74.         tempstr [ MAXLEN + 3 ],
  75.         targetfile [ MAXLEN ],
  76.         tempfilename [ MAXWORDLEN ] = "tmp$$$.$$$",
  77.         bar [ LINE_LEN + 1 ],
  78.         double_bar [ LINE_LEN + 1 ];
  79.  
  80.     FILE *fptr,
  81.         *tfile;
  82.     long wcount = 1L;
  83.      int columns,
  84.          interval;
  85.  
  86.        memset( bar, '-', LINE_LEN );
  87.        *( bar + LINE_LEN ) = NULL;
  88.        memset( double_bar, '=', LINE_LEN );
  89.        *( double_bar + LINE_LEN ) = NULL;
  90.  
  91.        /*************opening credits*************/
  92.        clrscr();
  93.        printf( double_bar );
  94.        strcpy( tempstr, ad );
  95.        center ( tempstr );
  96.        printf( tempstr );
  97.        printf( CR );
  98.        printf( double_bar );
  99.        printf( CR );
  100.        /****************************************/
  101.  
  102.  
  103.        /*   Create name of temp file to store derived words in  */
  104.        /*********************************************************/
  105.        strcpy( targetfile, tempfilename );
  106.        /*********************************************************/
  107.  
  108.        if( !( fptr = fopen( filename, "rt" ) ) )
  109.          {
  110.          printf( "\7\7\7Cannot open file to reformat!" );
  111.          exit( FILE_OPENING_ERROR );
  112.          }
  113.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  114.          exit( FILE_OPENING_ERROR + 1 );
  115.  
  116.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  117.          {
  118.          printf( "\7\7\7Cannot open temp working file!" );
  119.          exit ( FILE_OPENING_ERROR + 2 );
  120.          }
  121.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  122.          exit( FILE_OPENING_ERROR + 3 );
  123.  
  124.        /**************'Wait' Message************/
  125.        printf( CR CR );
  126.        printf( "WORKING...\n\n" );
  127.        printf( "Reformatting file %s.\n", filename );
  128.        printf( "It may take a few seconds, depending on file length.\n\n" );
  129.        /*****************************************/
  130.        columns = LINE_LEN / ( maxwl + WORDSPACING );
  131.       interval = maxwl + WORDSPACING;
  132.  
  133.          /*********************Main Loop*************/     
  134.  
  135.       printf( "\nThe file will be reformatted in %d columns.", columns );
  136.  
  137.           while( fgets( word, MAXLEN, fptr ) != NULL )
  138.  
  139.             if( strlen( word ) <= MAXWORDLEN )
  140.                {
  141.                *( word + strlen( word ) -1 ) = NULL;  //Gets rid of CR
  142.  
  143.                fprintf( tfile, "%-*s", interval, word );
  144.  
  145.                if( !( wcount % columns ) && wcount > columns - 1 )
  146.                   fprintf( tfile, CR );
  147. //               else
  148. //                  fprintf( tfile, "\t" );
  149.  
  150.                wcount++;
  151.                }
  152.             else
  153.                fprintf( tfile, "\n%s", word );
  154.  
  155.           /*******************************************/
  156.  
  157.           fcloseall();
  158.  
  159.           remove( filename );
  160.           rename( targetfile, filename );
  161.  
  162.           sprintf( tempstr,
  163.                  "The file %s has been reformatted.", filename );
  164.           center( tempstr );
  165.           printf( CR CR );
  166.           printf( tempstr );
  167.  
  168. }
  169.  
  170. int get_max_word_length( char *filename )
  171. {
  172.    int wlen,
  173.       maxwlen = 0; // Longest word in file
  174.    char word [ MAXLEN ];
  175.    FILE *fp;
  176.  
  177.       if( !( fp = fopen( filename, "rt" ) ) )
  178.             {
  179.             printf( "\7\7\7Cannot open file to reformat!" );
  180.             exit( FILE_OPENING_ERROR );
  181.             }
  182.  
  183.       clrscr();
  184.       printf( "Getting formatting information from file... \n" );
  185.       printf( "Please wait." );
  186.  
  187.       while( fgets( word, MAXLEN, fp ) != NULL )
  188.         {
  189.         wlen = strlen( word );
  190.         if( wlen > maxwlen && wlen < MAXWORDLEN )
  191.            maxwlen = wlen;  //Bump up to new value.
  192.         }
  193.  
  194.      return( maxwlen );
  195. }
  196.  
  197. void center( char *str )
  198. {
  199.    int padding;
  200.    char st [ LINE_LEN + INCREMENT ];
  201.  
  202.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  203.      memset( st, SPACE, padding );
  204.      *( st + padding ) = NULL;  //Terminate string
  205.      strcat( st, str );
  206.      strcpy( str, st );
  207.  
  208.      return;
  209. }
  210.